cap log close
clear all
set more off
set matsize 11000
set maxvar 120000

sysdir set PLUS "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\plus"

*Main data pathway globals, set directory to clean data folder
global results "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\ResultsRev\"
global cleandata "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\CleanData\"

cd "$cleandata"
use  clean_patientlevel_file_rev3.dta, clear 
keep if str_exogGPIDnew!=. & yr_str_exog_swap >= 2005 & yr_str_exog_swap<=2014


* largest mobility group 
a2group, individual(str_exogGPIDnew) unit(str_exogGPIDprev) groupvar(pair)
bys pair: ge size_pair = _N
drop if size_pair < 10 ///largest mobility group has 99%


merge m:1 str_exogGPIDnew using "$results/fe_addon2.dta"
drop _merge


ge death5 = mortality_5year
ge death2 = mortality_2year
for num 25 35 45 55: ge death5_X = mortality_5year if str_exog_age>=X
for num 25 35 45 55: ge death2_X = mortality_2year if str_exog_age>=X
for var heart cancer ext resp mental accident suicide homicide: ge X2y_55 = X_2y if str_exog_age>=55
for var npr2 npr_noer2 npr_in2 npr_out2 charlindex2 sl_days2: ge X55 = X if str_exog_age>=55


* doc characteristics
ge dr_long_list = dr_list_length_str_exog_swap > 1100 if dr_list_length_str_exog_swap!=.
ge dr_old = dr_age_str_exog_swap >=40 if dr_age_str_exog_swap!=.
ge dr_max_list =  dr_max_str_exog_swap> 1200 if  dr_max_str_exog_swap!=.
ge dr_capacity = dr_max_str_exog_swap-dr_list_length_str_exog_swap >=0


* 1 - observables
local predictors "dr_old dr_long_list dr_max_list dr_spec_str_exog_swap dr_list_length_str_exog_swap dr_male_str_exog_swap dr_share_str_exog_swap dr_group_str_exog_swap dr_max_str_exog_swap dr_capacity"	


lab var str_exog_age 	"Age at swap"
lab var male 			"Male"
lab var NORborn			"Born in Norway"
lab var hs 				"High School or Less"
lab var edu_yrs 		"Years of Eduaction"
lab var married 		"Married"
lab var lab_income5 	"Labor income"
lab var tot_income5 	"Total Income"
lab var unempben5		"Received Unemploment Benefits?"
lab var dwoverfor5  			"Any welfare benefit"	
lab var	mortality_2year 	"Mortality 2 years after"
lab var mortality_5year 	"Mortality 5 years after"

lab var	total_base "GP consultations"
lab var labtest_base 	"Lab tests"
lab var procedure_base 	"Surgery"
lab var sl_base  		"Sick Leave"
lab var reimburse_base 	"Annual Reimbursement cost"
lab var dnpr_base 				"Any Hospitalization"	


label var death2_55 	"2-years mortality for 55+"
label var bweight_2y 	"Birthweight"
label var heart2y_55 	"Mortality: heart conditions"
label var cancer2y_55 	"Mortality: cancer"
label var ext2y_55 		"Mortality: external conditions"
label var resp2y_55 	"Mortality: respiratory conditions"
label var mental2y_55 	"Mortality: mental health conditions"
label var accident2y_55	"Mortality: accident conditions"
label var sl_days255 	"Nb sick leave days"
label var npr255 		"Nb of hospitalizations"
label var npr_noer255 	"Nb of hospitalizations - not ER"
label var charlindex255 "Charlson Index"

bys str_exogGPIDnew: ge n = _n
* recode main VA measure (more VA - better doc, less mortality) 
ge newva = -afe_death2_55
local va "newva" 
su `va', d
gen stdva = (`va'-r(mean))/r(sd)
local va "stdva"
lab var stdva "Standardized VA"

keep if str_exogGPIDnew!=.


* -----------------------------------------------------------------------------
* stats (TABLES A3 A5, cols 1 to 4)
* -----------------------------------------------------------------------------
for var heart2y_55 cancer2y_55 ext2y_55 resp2y_55: ge _X = X if mortality_2year == 1
label var _heart2y_55 	"Mortality: heart conditions"
label var _cancer2y_55 	"Mortality: cancer"
label var _ext2y_55 	"Mortality: external conditions"
label var _resp2y_55 	"Mortality: respiratory conditions"

global health 	"mortality_2year _heart2y_55 _cancer2y_55 _ext2y_55 _resp2y_55 mortality_5year"
global demo 	"str_exog_age male NORborn hs edu_yrs married"
global use		"total_base dlabtest_base dprocedure_base dsl_base dreimburse_base dnpr_base"
global inc	 	"lab_income tot_income  dwoverfor total_days_sl_base"

replace total_days_sl_base = 0 if  total_days_sl_base==.

lab var	total_base "GP consultations"
lab var dlabtest_base 	"Lab tests"
lab var dprocedure_base 	"Surgery"
lab var dsl_base  		"Sick Leave"
lab var dreimburse_base "Reimbursement"
lab var dnpr_base		"Hospitalization"

* characteristics of indiv (all) 
eststo s1: estpost tabstat $health $demo $use $inc if  str_exog_age >= 55, s(n mean sd) columns(statistics)  
eststo s2: estpost tabstat $health $demo $use $inc if  str_exog_age >= 25, s(n mean sd) columns(statistics)  

esttab s1 s2 using "$results/s_indiv.tex", replace fragment cells("mean(fmt(2)) sd(fmt(2))") noobs nomtitle ///
varlabels (`e(labels)') varwidth(20) booktabs label mgroups("55 and Older" "25 and Older", pattern(1 1)) collabels("Mean" "SD" "N")
eststo clear


* characteristics of new gp
global dr_exo "dr_age_str_exog_swap dr_spec_str_exog_swap dr_list_length_str_exog_swap dr_male_str_exog_swap dr_share_str_exog_swap dr_group_str_exog_swap dr_max_str_exog_swap"
global patient_exo "total_base dlabtest_base dprocedure_base dsl_base"	

preserve
keep if str_exog_age >= 55 & stdva!=.
collapse  $dr_exo $patient_exo, by(str_exogGPIDnew)
for var $patient_exo: replace X = 0 if X==.

lab var	total_base 		"GP consultations"
lab var dlabtest_base 	"Lab tests"
lab var dprocedure_base "Surgery"
lab var dsl_base  		"Sick Leave"

lab var dr_age_str_exog_swap 			"GP Age" 
lab var dr_spec_str_exog_swap 			"GP specialist" 
lab var dr_list_length_str_exog_swap 	"GP list lenght"
lab var dr_male_str_exog_swap  			"GP is male"
lab var dr_share_str_exog_swap 			"GP has a shared list"
lab var dr_group_str_exog_swap 			"GP in group practice"
lab var dr_max_str_exog_swap  			"GP max list lenght"

eststo s1: estpost tabstat $dr_exo $patient_exo, s(n mean sd) columns(statistics)  
restore

preserve
keep if str_exog_age >= 25
collapse  $dr_exo $patient_exo, by(str_exogGPIDnew)
for var $patient_exo: replace X = 0 if X==.

lab var	total_base 		"GP consultations"
lab var dlabtest_base 	"Lab tests"
lab var dprocedure_base "Surgery"
lab var dsl_base  		"Sick Leave"


lab var dr_age_str_exog_swap 			"GP Age" 
lab var dr_spec_str_exog_swap 			"GP specialist" 
lab var dr_list_length_str_exog_swap 	"GP list lenght"
lab var dr_male_str_exog_swap 			"GP is male"
lab var dr_share_str_exog_swap 			"GP has a shared list"
lab var dr_group_str_exog_swap 			"GP in group practice"
lab var dr_max_str_exog_swap  			"GP max list lenght"

eststo s2: estpost tabstat $dr_exo $patient_exo, s(n mean sd) columns(statistics)  
restore

esttab s1 s2 using "$results/s_gp.tex", replace fragment cells("mean(fmt(2)) sd(fmt(2))") noobs nomtitle ///
varlabels (`e(labels)') varwidth(20) booktabs label mgroups("55 and Older" "25 and Older", pattern(1 1)) collabels("Mean" "SD")


* -----------------------------------------------------------------------------
* correlates of VA (TABLE 3)
* -----------------------------------------------------------------------------
keep if stdva!=.
global dr_exo "dr_age_str_exog_swap dr_male_str_exog_swap dr_spec_str_exog_swap dr_list_length_str_exog_swap dr_share_str_exog_swap dr_group_str_exog_swap"

collapse (mean)  stdva $dr_exo, by(str_exogGPIDnew)
merge 1:1 str_exogGPIDnew using "khurh_gp_info.dta"
keep if _m!=2

global type "total consult simple_contact simple_nopinperson procedure life_style_adv blood_test labtest sl referral reimburse"

foreach var in $type {
	replace `var' = 0 if `var'==.
}


lab var	total 			"GP consultations"
lab var labtest			"Lab tests"
lab var life_style_adv 	"Advising"
lab var blood_test		"Blood Test"
lab var procedure 		"Surgery"
lab var sl  			"Sick Leave"
lab var reimburse 		"Annual Reimbursement cost (in 100NOK)"
lab var referral 		"Referrals"

lab var dr_age_str_exog_swap 			"GP Age" 
lab var dr_spec_str_exog_swap 			"GP specialist" 
lab var dr_list_length_str_exog_swap 	"GP list lenght  (in 1,000)"
lab var dr_male_str_exog_swap  			"GP is male"
lab var dr_share_str_exog_swap 			"GP has a shared list"
lab var dr_group_str_exog_swap 			"GP in group practice"
lab var stdva							"VA measure - 2 years mortality rate 55+"


replace reimburse =reimburse/1000
replace dr_list_length_str_exog_swap = dr_list_length_str_exog_swap/1000

global type "total procedure life_style_adv blood_test labtest sl referral reimburse"

eststo c1: reg stdva $dr_exo, robust  
testparm $dr_exo
estadd scalar pv = r(p)
eststo c1

eststo c2: reg stdva $type, robust  
testparm $type
estadd scalar pv1 = r(p)
eststo c2

eststo c3: reg stdva $dr_exo $type, robust  
testparm $dr_exo $type
estadd scalar pv = r(p)
eststo c3
testparm $type
estadd scalar pv1 = r(p)
eststo c3
esttab c1 c2 c3 using "$results/correlates.tex", booktabs legend replace label b(3) se(3) r2 star(* 0.1 ** 0.05 *** 0.01) stats(emp pv pv1 emp r2 N, fmt(%9.3g)) 

eststo clear
